package android.bignerdranch.shiyan4;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;

public class MyService extends Service {

    private int count=0;
    private boolean quit=false;
    private MyServiceBinder myServiceBinder=new MyServiceBinder();
    public MyService() {

    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("--Service is Binded--");
        return myServiceBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //创建新Thread，如果service启动每秒钟count++，如果quit为真则退出
        new Thread(()->
        {
           while (!quit) {
               try {
                   Thread.sleep(1000);
               }catch (InterruptedException e){
                   e.printStackTrace();
               }
               MyService.this.count++;
           }
        }).start();
    }


    class MyServiceBinder extends Binder {
        public int getCount() {
            return MyService.this.count;
        }
    }

        @Override
        public void onDestroy() {
            super.onDestroy();
            quit = true;
        }

       }
